import unittest
import optparse
from os.path import join
import sys
from Autotests.TestRunner import HTMLTestRunner

from Config.RunConfig import RunConfig, Options
from Config import commonvars
from Autotests.Cloud.executor import ActionExecutor as executor


parser = optparse.OptionParser()
parser.add_option('-s', '--startDir', type = 'string', dest = 'startDir', help = 'root dir for tests discover')
parser.add_option('-p', '--pattern', type = 'string', dest = 'pattern', default = "*.py", help = 'test files pattern')


def main(rootPath, pattern):
    loader = unittest.TestLoader()
    tests = loader.discover(rootPath, pattern)
    runconfig = RunConfig()
    testRunner = HTMLTestRunner.HTMLTestRunner()

    wsdls = {join(commonvars.wsdlRootDir, "13.5-svc_13_5_0.wsdl"): "13.5",
             runconfig.get(Options.Soap.manservWsdlPath): "base"}

    for wsdl in wsdls.keys():
        runconfig.setMansvcWsdlPath(wsdl)
        reportName = "soap_report_%s_protocol.html" % wsdls[wsdl]
        reportTitle = "Clients tests result for %s protocol for %s" % (wsdls[wsdl], executor().getServerInfo().version)
        with open(join(commonvars.testsLogsRootDir, reportName), 'w') as reportFile:
            testRunner.title = reportTitle
            testRunner.stream = reportFile
            testRunner.run(tests)


if __name__ == '__main__':
    options, args = parser.parse_args()
    if not options.startDir:
        sys.exit("Error: start Dir is required")
    else:
        main(options.startDir, options.pattern)